//
//  NSString+URL.m
//  测试
//
//  Created by 李李善 on 2017/4/14.
//  Copyright © 2017年 森之源. All rights reserved.
//

#import "NSString+URL.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (URL)
-(NSString *)removedByUTF8String
{
    return self;
}

-(NSString *)strUTF8String{
    
    return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<>+"].invertedSet];
//    return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}



-(NSURL *)onURLByStrig{
    NSURL *url = [NSURL URLWithString:self];
    return url?url:nil;
}
- (NSString*) sha
{
    
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
    
}

-(NSString *)onAbsoluteURLString{
    NSString *URLString = [NSString stringWithFormat:@"%@/%@",host,self];
    return URLString?URLString:nil;
}



- (NSString *)md5
{
    const char* original_str=[self UTF8String];
    //CC_MD5_DIGEST_LENGTH = 16
    unsigned char digist[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(original_str, (int)strlen(original_str), digist);
    
    NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10];
    
    for(int  i =0; i<CC_MD5_DIGEST_LENGTH;i++){
        //小写x表示输出的是小写MD5，大写X表示输出的是大写MD5
        [outPutStr appendFormat:@"%02x", digist[i]];
    }
    return [outPutStr lowercaseString];
    
}

@end





@implementation NSString (UrlEncode)

- (NSString *)urlEncode {
    return [self urlEncodeUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
    return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                 (__bridge CFStringRef)self,NULL,(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                                 CFStringConvertNSStringEncodingToEncoding(encoding));
}

- (NSString *)urlDecode {
    return [self urlDecodeUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)urlDecodeUsingEncoding:(NSStringEncoding)encoding {
    return (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                 (__bridge CFStringRef)self,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(encoding));
}

- (NSDictionary *)dictionaryFromURLParameters
{
    NSMutableDictionary * parametersDictionary = [NSMutableDictionary dictionary];
    NSURL *url =[[NSURL alloc]initWithString:self];
    NSArray *queryComponents = [url.query componentsSeparatedByString:@"&"];
    for (NSString * queryComponent in queryComponents) {
        NSString * key = [queryComponent componentsSeparatedByString:@"="].firstObject;
        NSString * value = [queryComponent substringFromIndex:(key.length + 1)];
        value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [parametersDictionary setObject:value forKey:key];
    }
    
    
    return parametersDictionary;
    
    
}

@end

